package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;

import static java.lang.Integer.parseInt;

/**
 * Created with IntelliJ IDEA.
 * User: Thoughtworker
 * Date: 8/21/13
 * Time: 10:55 AM
 * To change this template use File | Settings | File Templates.
 */
public class Menu {
    private BufferedReader bufferedReader;
    private PrintStream printStream;
    private String[] options;
    private Option[] optionsEnum;
    private BibliotecaApp app;


    public Menu(BufferedReader bufferedReader, PrintStream printStream)
    {
        Option.VIEWBOOKS.setID(1);
        Option.RESERVEBOOK.setID(2);
        Option.CHECKACCOUNTSTATUS.setID(3);
        Option.VIEWMOVIES.setID(4);
        Option.LOGIN.setID(5);
        optionsEnum = new Option[]{Option.VIEWBOOKS, Option.RESERVEBOOK, Option.CHECKACCOUNTSTATUS, Option.VIEWMOVIES, Option.LOGIN};
        this.printStream = printStream;
        this.bufferedReader = bufferedReader;
    }

    public void setApp(BibliotecaApp app)
    {
        this.app = app;

    }

    public void welcome() {
        printStream.println("Welcome!");
    }

    public void showMenuOptions() {
        for(int i =0; i< optionsEnum.length; i++)
        {
            printStream.println("[" + (optionsEnum[i].getID()) + "] " + optionsEnum[i].getLabel() );
        }

    }

    public int readMenuOption()  {
        int result = 0;
        try {
            result = parseInt(bufferedReader.readLine()) ;
            if(result <= 0 || result > optionsEnum.length)
                throw new Exception();
        } catch (Exception e) {
            printStream.println("Select a valid option!!");
            result = 0;
        }

        return result;
    }

    public void processMenuOption(int option) throws IOException {
        if(option == Option.VIEWBOOKS.getID())
        {
            app.showBooks();
        }
        else if(option == Option.RESERVEBOOK.getID()) {

            app.showBooks();
            try {
                app.reserveBook(readBookName());
            } catch (IOException e) {
                app.reserveBook(null);  //To change body of catch statement use File | Settings | File Templates.
            }

        }
        else if(option == Option.CHECKACCOUNTSTATUS.getID())
        {
            app.checkMembership();
        }
        else if(option == Option.VIEWMOVIES.getID()){
            app.showMovies();
        }else if(option == Option.LOGIN.getID()){

            app.verifyUser(readUserName(), readPassword());

        }
    }

    public String readUserName() throws IOException {
        printStream.println("Enter Username:");
        return  bufferedReader.readLine();
    }
    public String readPassword() throws IOException {
        printStream.println("Enter Password:");
        return bufferedReader.readLine();

    }


    public String readBookName() throws IOException {
        printStream.println("Enter name of book to reserve:");
        return bufferedReader.readLine();
    }


}
